#!/bin/sh
# getruckusapnames
# list all APs on the Ruckus ZoneDirector
# including mac address, IP, model + description
#
#William Burns

DEBUG=false
#DEBUG=true

SORT=false
SORT=true

UNDERSPACE=false

SEDFORMAT=false
#SEDFORMAT=true

DELIM="~"

COMMUNITY=public

#Change CONTROLLERADDRESS to your own ZD controller address
CONTROLLERADDRESS=192.168.1.99
BACKUPCONTROLLERADDRESS=192.168.1.100

STATUS=`snmpget -On -m "" -v2c -c $COMMUNITY $CONTROLLERADDRESS 1.3.6.1.4.1.25053.1.2.1.1.1.1.30.0`
STATUS=`echo $STATUS | sed -s 's/^.*INTEGER: //'`

$DEBUG && echo STATUS is `echo "$STATUS"|od -c` >&2

if [ "$STATUS" = "2" ]
then
  CONTROLLERADDRESS="$BACKUPCONTROLLERADDRESS"
fi


OID="1.3.6.1.4.1.25053.1.2.2.4.1.1.1.1"


while echo $1 |grep "^-" >/dev/null
do
  case $1 in
    "--controller" )
      CONTROLLERADDRESS=$2
      shift;shift
      ;;
    "--community" )
      COMMUNITY=$2
      shift;shift
      ;;
    "--sedformat" )
      SEDFORMAT=true
      shift
      ;;
    "--underspace" )
      UNDERSPACE=true
      shift
      ;;
    "--debug" )
      DEBUG=true
      shift
      ;;
    "--help" | "-?" | "-h" | * )
      echo Usage:
      echo $0 "[OPTIONS] <IP-Address>"
      echo OPTIONS:
      echo "--help -? -h"
      echo "--controller <controlleraddress> ;# query the IP of the specified controller"
      echo "--community <communitystring>    ;# use the specified community string"
      echo "--sedformat   ;# produce sed commands to convert AP MAC to AP Name"
      echo "--underspace  ;# convert spaces to underscores in output"
      echo "--debug       ;# turn on debugging output"
      exit
      ;;
  esac
done



{
$DEBUG && echo "snmpwalk -On -c $COMMUNITY -v2c -Pe -m "" $CONTROLLERADDRESS $OID" >&2
snmpwalk -On -c $COMMUNITY -v2c -Pe -m "" $CONTROLLERADDRESS $OID
} \
| sed -e 's/^.*'${OID}'.//' \
| sed -e 's/\./ /' \
| sort -t" " -k2,2n -k1,1n \
| { while read FLDNUM RECNUM EQ FLDTYPE VALUE
do
  #echo $FLDNUM $RECNUM $EQ $FLDTYPE $VALUE
  if [ "$FLDNUM" = "2" ]
  then
    if [ -z "$MACADDRESS" ]
    then
      ! $SEDFORMAT && echo MACADDRESS, APNAME, IPADDRESS, MODEL, DESCRIPTION >&2
    else
      echo "${MACADDRESS}${DELIM}${APNAME}${DELIM}${IPADDRESS}${DELIM}${MODEL}${DELIM}${DESCRIPTION}"
    fi

    MACADDRESS=$VALUE
    MACADDRESS=`echo $MACADDRESS| tr -s " " ":"`
  fi

  if [ "$FLDNUM" = "5" ]
  then
    APNAME=$VALUE
  fi

  if [ "$FLDNUM" = "16" ]
  then
    IPADDRESS=$VALUE
  fi

  if [ "$FLDNUM" = "4" ]
  then
    MODEL=$VALUE
  fi

  if [ "$FLDNUM" = "6" ]
  then
    DESCRIPTION=$VALUE
  fi

done
echo "${MACADDRESS}${DELIM}${APNAME}${DELIM}${IPADDRESS}${DELIM}${MODEL}${DELIM}${DESCRIPTION}"
} \
| if $UNDERSPACE
then
  sed 's/ /_/g' | sed 's/~/ /g'
else
  sed 's/~/ /g'
fi \
| if $SORT
then
  sort -k2,2
else
  cat
fi \
| if $SEDFORMAT
then
  echo -e 'sed\c'
  while read MAC HOSTNAME JUNK
  do
    echo ' \'
    echo -e "  -e 's/${MAC}/${HOSTNAME}/I'\c"
  done \
  | tr -d '"'
  echo
else
  cat
fi
